package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzSettingInCheck;
import com.jy.adscm.operation.mscyb.service.JqbzSettingInCheckService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-06-10
 */
@RestController
@RequestMapping("/adscm/operation/jqbz/jqbzSettingInCheck")
public class JqbzSettingInCheckController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqbzSettingInCheckService jqbzSettingInCheckService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqbzSettingInCheck jqbzSettingInCheck, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbzSettingInCheck.setIsEnable(1);
        jqbzSettingInCheck.setCreator(token.getUser().getId().toString());
        jqbzSettingInCheck.setCreateTime(new Date());
        Wrapper<JqbzSettingInCheck> wrapper =new EntityWrapper<>();
        wrapper.eq("pid",jqbzSettingInCheck.getPid());
        wrapper.orderBy("rank",false);
        wrapper.last("LIMIT 1");
        List<JqbzSettingInCheck> jqbzSettingInChecks = jqbzSettingInCheckService.selectList(wrapper);
        Integer rank=1;
        if (jqbzSettingInChecks!=null && jqbzSettingInChecks.size()>0 ){
            JqbzSettingInCheck jqbzSettingInCheck1 = jqbzSettingInChecks.get(0);
            rank=jqbzSettingInCheck1.getRank()+1;
        }
        jqbzSettingInCheck.setRank(rank);
        boolean insert = jqbzSettingInCheckService.insert(jqbzSettingInCheck);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqbzSettingInCheck jqbzSettingInCheck, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbzSettingInCheck.setCreator(token.getUser().getId().toString());
        jqbzSettingInCheck.setCreateTime(new Date());
        boolean insert = jqbzSettingInCheckService.updateById(jqbzSettingInCheck);
        return new SimpleCoreResponse("200","修改成功",insert);
    }
    @RequestMapping("selectByPid/{pid}")
    public SimpleCoreResponse selectByPid(@PathVariable String pid, HttpServletRequest request) throws AdscmSystemException {
        List<JqbzSettingInCheck> jqbzSettingInCheck=jqbzSettingInCheckService.selectByPid(pid);
        return new SimpleCoreResponse("200","查询成功",jqbzSettingInCheck);
    }
}

